<template>
  <el-dialog :title="!dataForm.userId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="登录账号（6-50字符）" prop="username">
        <el-input v-model="dataForm.username" placeholder="登录账号（6-50字符）"></el-input>
      </el-form-item>
      <el-form-item label="加密后的密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="加密后的密码"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <!-- <el-input v-model="dataForm.gender" placeholder="性别"></el-input> -->
        <el-select v-model="dataForm.gender" placeholder="请选择">
          <el-option v-for="item in genderOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>

      </el-form-item>
      <el-form-item label="绑定邮箱（唯一）" prop="email">
        <el-input v-model="dataForm.email" placeholder="绑定邮箱（唯一）"></el-input>
      </el-form-item>
      <el-form-item label="头像URL地址" prop="avatar">
        <el-input v-model="dataForm.avatar" placeholder="头像URL地址"></el-input>
      </el-form-item>
      <el-form-item label="账户状态" prop="status">
        <!-- <el-input v-model="dataForm.status" placeholder="账户状态"></el-input> -->
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLogin">
        <!-- <el-input v-model="dataForm.lastLogin" placeholder="最后登录时间"></el-input> -->
        <el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="dataForm.lastLogin" type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="注册时间" prop="createdAt">
        <!-- <el-input v-model="dataForm.createdAt" placeholder="注册时间"></el-input> -->
        <el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="dataForm.createdAt" type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        userId: 0,
        username: '',
        password: '',
        gender: '',
        phone: '',
        email: '',
        avatar: '',
        status: '',
        lastLogin: '',
        createdAt: ''
      },
      dataRule: {
        username: [
          { required: true, message: '登录账号（6-50字符）不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '加密后的密码不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '绑定邮箱（唯一）不能为空', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '头像URL地址不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '账户状态不能为空', trigger: 'blur' }
        ],
        lastLogin: [
          { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
        ],
        createdAt: [
          { required: true, message: '注册时间不能为空', trigger: 'blur' }
        ]
      },

      genderOptions: [{
        value: 1,
        label: '男'
      }, {
        value: 0,
        label: '女'
      }],
      statusOptions: [{
        value: 1,
        label: '可用'
      }, {
        value: 0,
        label: '冻结'
      }],
    }
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.userId) {
          this.$http({
            url: this.$http.adornUrl(`/member/user/info/${this.dataForm.userId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.username = data.data.username
              this.dataForm.password = data.data.password
              this.dataForm.gender = data.data.gender
              this.dataForm.phone = data.data.phone
              this.dataForm.email = data.data.email
              this.dataForm.avatar = data.data.avatar
              this.dataForm.status = data.data.status
              this.dataForm.lastLogin = data.data.lastLogin
              this.dataForm.createdAt = data.data.createdAt
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/member/user/${!this.dataForm.userId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'userId': this.dataForm.userId || undefined,
              'username': this.dataForm.username,
              'password': this.dataForm.password,
              'gender': this.dataForm.gender,
              'phone': this.dataForm.phone,
              'email': this.dataForm.email,
              'avatar': this.dataForm.avatar,
              'status': this.dataForm.status,
              'lastLogin': this.dataForm.lastLogin,
              'createdAt': this.dataForm.createdAt
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
